"""Row T/F-Test

.. helpdoc::
Calculates the degradation of RNA from a set of Affy data.

"""

"""<widgetXML>
    <name>Row T/F-test<!-- [REQUIRED] title --></name>
    <icon>default.png</icon>
    <tags>
        <tag>Affy</tag>
    </tags>
    <summary>Calculates T or F test statistics from an eSet object, can be used in conjunction with GSEA analysis.</summary>
    <author>
        <authorname>Kyle R Covington</authorname>
        <authorcontact>kyle@red-r.org</authorcontact>
    </author>
</widgetXML>
"""

"""
<name>Row T/F-Test</name>
<author>Generated using Widget Maker written by Kyle R. Covington</author>
<RFunctions>genefilter:rowttest, genefilter:rowFtest</RFunctions>
<tags>Affy QC</tags>
<icon></icon>
<inputWidgets></inputWidgets>
<outputWidgets>affy_RedRplotAffyRNADeg, affy_RedRsummaryAffyRNADeg</outputWidgets>
"""
from OWRpy import * 
import redRGUI
import signals
class gseaStart(OWRpy): 
    settingsList = []
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        self.require_librarys(["genefilter"])
        self.setRvariableNames(["rtt"])
        self.data = {}
        self.RFunctionParam_abatch = ''
        
        """.. rrsignals::
        .. helpdoc::
        Input is an Eset containing expression and annotation data, use other tools to generate or read expression data into this format.
        """
        self.inputs.addInput('ab', "Affy Batch", signals.affy.REset, self.processabatch)
        
        
        """.. rrsignals::"""
        self.outputs.addOutput('tstats', "Statistics", signals.base.RVector)
        
        self.GUIParam_criteriaSelect = redRGUI.base.listBox(self.controlArea, label = "Valid Comparison Criteria", callback = self.commitFunction)
        self.GUIParam_testType = redRGUI.base.radioButtons(self.controlArea, label = "Test Type", buttons = [('rowttests', 'T statistic'), ('rowFtests', 'F statistic')], setChecked = 'rowttests')
        
        redRGUI.base.commitButton(self.bottomAreaRight, "Commit", callback = self.commitFunction)
    def processabatch(self, data):
        if data:
            self.RFunctionParam_abatch=data.getData()
            
            # process the parameters in the phenoData and check that they are valid for comparison.
            pDataNames = self.R('names(pData(%s))' % self.RFunctionParam_abatch, wantType = redR.LIST)
            validpDataCols = []
            for n in pDataNames:
                if self.R('length(levels(as.factor(pData(%s)$%s)))' % (self.RFunctionParam_abatch, n)) == 2:
                    validpDataCols.append(n)
            self.GUIParam_criteriaSelect.update(validpDataCols)
            
        else:
            self.RFunctionParam_abatch=''
    def commitFunction(self):
        if unicode(self.RFunctionParam_abatch) == '': return
        
        if self.GUIParam_testType.getCheckedId() == 'rowttests':
            self.R('%(rtt)s<-rowttests(%(data)s, fac = pData(%(data)s)$%(fac)s, tstatOnly = TRUE)' % {'rtt':self.Rvariables['rtt'], 'data':self.RFunctionParam_abatch, 'fac':self.GUIParam_criteriaSelect.selectedIds()[0]}, wantType = redR.NOCONVERSION)
        elif self.GUIParam_testType.getCheckedId() == 'rowFtests':
            self.R('%(rtt)s<-rowFtests(%(data)s, fac = pData(%(data)s)$%(fac)s, var.equal = FALSE)' % {'rtt':self.Rvariables['rtt'], 'data':self.RFunctionParam_abatch, 'fac':self.GUIParam_criteriaSelect.selectedIds()[0]}, wantType = redR.NOCONVERSION)
        self.R('names(%(rtt)s) <- featureNames(%(data)s)' % {'rtt':self.Rvariables['rtt'], 'data':self.RFunctionParam_abatch}, wantType = redR.NOCONVERSION)
        newData = signals.base.RVector(self, data = self.Rvariables["rtt"]) # moment of variable creation, no preexisting data set.  To pass forward the data that was received in the input uncomment the next line.
        self.rSend("tstats", newData)
